	var untils = {
		//获取url参数
		getQueryString: function (name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);   //返回要匹配的参数
			return r ? decodeURIComponent(r[2]) : null;
		},
	
		counttimer: null,
		standtimer : null,
		monkeytimer: null,
		base_url: "http://activity.gzl.com.cn",
		secondtime_nz: 210,
		usr_sign : function(){
			return untils.getQueryString("usr_sign");
		},
		usr_nonce : function () {
			return untils.getQueryString("usr_nonce");
		},
		usr_ts : function () {
			return untils.getQueryString("usr_ts");
		},
	
		// 是否是微信打开
		isWeiXin: function () {
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true;
			} else {
				return false;
			}
		},
	
		//是否登录
		isLogin : function (toUrl) { 
			var loginUrl = "http://paytest.ecwalk.com/account/login.html?type=1&goToUrl=" + toUrl;
			var usr_sign = this.getQueryString("usr_sign");
			var usr_nonce = this.getQueryString("usr_nonce");
			var usr_ts = this.getQueryString("usr_ts");
			var usr_info = this.getQueryString("wxUserInfo");
			var login_stamp = parseInt(new Date().getTime() / 1000);
			var checkToken = {
				usr_sign: usr_sign,
				usr_nonce: usr_nonce,
				usr_ts: usr_ts,
				usr_info: usr_info,
				login_stamp: login_stamp
			};
			//已经登录过
			if(usr_sign&&usr_nonce&&usr_ts&&usr_info&&localStorage.check_token==undefined){
				localStorage.check_token = JSON.stringify(checkToken);
			}else{
				try{
					var check_token = JSON.parse(localStorage.check_token);
				}catch(err) {
					localStorage.removeItem("check_token");
					weui.alert("您还没有登录，请登录后再参与此活动！", function () {
						window.location = loginUrl;
					});
				}
				if(localStorage.check_token==undefined||check_token.usr_sign==undefined||check_token.usr_nonce==undefined||check_token.usr_ts==undefined||check_token.login_stamp==undefined||check_token.usr_info==undefined){   
					localStorage.removeItem("check_token");
					weui.alert("您还没有登录，请登录后再参与此活动！", function () {
						window.location = loginUrl;
					});
				}else{
					if(checkToken.usr_sign!=null){
						if(checkToken.usr_sign==check_token.usr_sign){
							var this_stamp = parseInt(new Date().getTime() / 1000);
							var time_min = parseInt(this_stamp-check_token.login_stamp)/ 3600;  // 
							if(time_min>2){
								window.location = loginUrl;
							}
						}else{
							localStorage.setItem("check_token" , JSON.stringify(checkToken) );
						}
					}else{
						var this_stamp = parseInt(new Date().getTime() / 1000);
						var time_min = parseInt(this_stamp-check_token.login_stamp)/ 3600;  // 
						if(time_min>2){
							window.location = loginUrl;
						}
					}
				}
			}
		},
	
		//获取游戏机会
		getTicket: function (activityId, channel, callback){
			// var check_token = JSON.parse(localStorage.check_token);
			// console.log(this.usr_sign(), this.usr_nonce(), this.usr_ts())
			var that = this;
			var gameticket = this.base_url + "/game/getTicket/" + activityId + "?channel=" + channel;
			$.ajax({
				type: "GET",
				contentType: "application/json",
				dataType:'json',
				data:{
					usr_sign: that.usr_sign(),
					usr_nonce: that.usr_nonce(),
					usr_ts: that.usr_ts()
				},
				url: gameticket,
				success: function (data) {
					console.log('success：',data);
					callback(data);
				},
				error: function (data) {
					console.log('err:'+data.data);
					alert("哎呀，请求出错啦！");
				}
			});
		},
	
		//开始游戏
		playgame: function (activityId, channel, ticket, ticketback){
			// var check_token = JSON.parse(localStorage.check_token);
			// console.log('ticket:',ticket)
			var playgameurl = this.base_url + "/game/play/" + activityId + "?channel=" + channel + "&nonce=" + ticket.nonce + "&timestamp=" + ticket.timestamp + "&token=" + ticket.token + "&usr_sign=" + encodeURIComponent(this.usr_sign()) + "&usr_nonce=" + this.usr_nonce() + "&usr_ts=" + this.usr_ts();
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded",
				dataType:'json',
				url: playgameurl,
				success: function (data) {
					// console.log('success：',data);
					ticketback(data);
				},
				error: function (data) {
					console.log('err:'+data);
					// alert("哎呀，网络出错啦！");
				}
			});
		},
	
		sharegame: function (activityId, channel,sharecallback) {
			// var check_token = JSON.parse(localStorage.check_token);
			var shareurl = this.base_url + "/game/share/" + activityId + "?channel=" + channel + "&usr_sign=" + encodeURIComponent(this.usr_sign()) + " &usr_nonce=" + this.usr_nonce() + "&usr_ts=" + this.usr_ts();
			$.ajax({
				type: "POST",
				contentType: "application/json",
				dataType: 'json',
				url: shareurl,
				success: function (sharedata) {
					// console.log('share-success：' + sharedata);
					sharecallback(sharedata);
				},
				error: function (sharedataerr) {
					alert('share-err:' + sharedataerr.errorInfo);
				}
			});
		},
	
		//提交分数
		gamesubmit: function (activityId, channel, ticket_obj, score, gamesubmitback){
			var gamesubmitturl = this.base_url + "/game/submitResult/" + activityId + "?channel=" + channel;
			var that = this;
			var result = {
				"none": 'nothing'
			}
			var s = JSON.stringify(result);
			// s = s.toString();
			$.ajax({
				type: "POST",
				contentType: "application/json",
				dataType: 'json',
				data: s.toString(),
				url: gamesubmitturl + "&nonce=" + ticket_obj.nonce + "&timestamp=" + ticket_obj.timestamp + "&token=" + ticket_obj.token + "&usr_sign=" + encodeURIComponent(that.usr_sign()) + "&usr_nonce=" + that.usr_nonce() + "&usr_ts=" + that.usr_ts() + "&score=" + score,
				success: function (data) {
					// console.log('success：', data);
					gamesubmitback(data);
				},
				error: function (data) {
					console.log('err:' + data);
					alert("哎呀，网络出错啦！");
				}
			});
		},
	
		//抽奖机会
		getluckyticket: function (activityId, channel, checkToken, callback) {
			// var check_token = JSON.parse(check_token);
			var check_token = JSON.parse(checkToken);
			// console.log('check_token:',check_token);
			var lucky_url = this.base_url + "/getTicket/" + activityId;
			var that = this;
			$.ajax({
				type: 'GET',
				contentType: false,
				dataType: 'json',
				data: {
					channel: channel,
					usr_sign: check_token.usr_sign,
					usr_nonce: check_token.usr_nonce,
					usr_ts: check_token.usr_ts
				},
				url: lucky_url,
				success: function (data) {
					// console.log('getluckyticket:', data);
					if (data.message == "不能参与此抽奖活动") {
						weui.alert("请重新玩游戏获得抽奖机会哦！");
					} else if (data.message == "抽奖时发生系统异常") {
						weui.alert("系统繁忙！请稍后重试！");
					} else {
						that.getlucky(check_token, data.data, activityId, channel, callback)
					}
				},
				error: function (err) {
					console.log(err);
					// weui.alert("网络出错！请稍后重试！");
				}
			});
		},
	
		//抽奖
		getlucky: function (check_token, data, activityId, channel, callback) {
			// var check_token = JSON.parse(localStorage.check_token);
			var lucky_url = this.base_url + "/play/" + activityId + "?channel=" + channel + "&nonce=" + data.nonce + "&timestamp=" + data.timestamp + "&token=" + data.token + "&usr_sign=" + encodeURIComponent(check_token.usr_sign) + "&usr_nonce=" + check_token.usr_nonce + "&usr_ts=" + check_token.usr_ts;
			$.ajax({
				type: 'POST',
				url: lucky_url,
				dataType: 'json',
				data: JSON.stringify({ "actiDynamicParams": '' }),
				contentType: 'application/json',
				processData: false,
				success: function (data) {
					// console.log('getlucky:', data);
					if (data.message == "抽奖时发生系统异常") {
						weui.alert("系统繁忙！请稍后重试！");
					} else {
						callback(data.data);
					}
				},
				error: function () {
				}
			});
		},
	
		//提交分数
		submitresult: function (activityId, channel, ticket, checkToken, userinfo, submitback) {
			var ticket_obj = JSON.parse(ticket);
			var check_token = JSON.parse(checkToken);
			// console.log(ticket_obj, check_token,userinfo);
			var submitresulturl = this.base_url + "/game/submitResult/" + activityId + "?channel=" + channel;
			var result = {
				"username": userinfo.username,
				"userphone": userinfo.userphone
			}
			var s = JSON.stringify(result);
			// s = s.toString();
			// console.log('sssss=',s);
			$.ajax({
				type: "POST",
				contentType: "application/json",
				dataType: 'json',
				data: s.toString(),
				url: submitresulturl + "&nonce=" + ticket_obj.nonce + "&timestamp=" + ticket_obj.timestamp + "&token=" + ticket_obj.token + "&usr_sign=" + encodeURIComponent(check_token.usr_sign) + "&usr_nonce=" + check_token.usr_nonce + "&usr_ts=" + check_token.usr_ts + "&score=" + userinfo.score,
				success: function (data) {
					// console.log('success：', data);
					submitback(data);
				},
				error: function (data) {
					console.log('err:' + data);
					alert("哎呀，网络出错啦！");
				}
			});
		},

		diaoyuresult: function (checkToken, userinfo, submitback){
			var diaoyu_url = this.base_url + "/tmp/gm/diaoyu/submitScore";
			var check_token = JSON.parse(checkToken);
			// console.log(check_token, userinfo);
			var userinfo_all = userinfo.userphone + userinfo.username;
			$.ajax({
				type: "POST",
				contentType: "application/x-www-form-urlencoded",
				dataType: 'json',
				data:{
					usr_sign: check_token.usr_sign,
					usr_nonce: check_token.usr_nonce,
					usr_ts: check_token.usr_ts,
					score: userinfo.score,
					contactNo: userinfo_all
				},
				url: diaoyu_url,
				success: function (data) {
					// console.log('success：', data);
					submitback(data);
				},
				error: function (data) {
					console.log('err:' + data);
					alert("哎呀，网络出错啦！");
				}
			});
		},
	
		//微信分享
		weChatConfig : function(url , txt , shareback){
			$.ajax({
				type: "GET",
				contentType: "application/json",
				async: true,
				data:{url:window.location.href},
				url: "http://weixin.gzl.cn/jssdkconfig/getConfig.json",
				success: function (data) {
					wx.config({
						debug: false, 
						appId: data.appId, // 必填，公众号的唯一标识
						timestamp: data.timestamp, // 必填，生成签名的时间戳
						nonceStr: data.noncestr, // 必填，生成签名的随机串
						signature: data.signature,// 必填，签名，见附录1
						jsApiList: ['checkJsApi','onMenuShareAppMessage','onMenuShareTimeline'] 
					})
				},
				error: function (data) {
				   if (XMLHttpRequest.status == 500) {
					   var result = eval("(" + XMLHttpRequest.responseText + ")");
					   $.weui.alert(result.jsonError.errorText);
				   }
					console.log(data);
				}
			});
				
			wx.ready(function(){
				//获取微信分享到朋友
				wx.onMenuShareAppMessage({
					desc: txt, // 分享描述
					link: url, // 分享链接
					imgUrl: "http://tpn.gzl.com.cn/static/game/runningMonkey/image/card-monkey.png", // 分享图标
					type: '', 
					dataUrl: '', 
					success: function () { 
						shareback();
					},
					cancel: function () { 
	
					}
				});
				
				//分享朋友圈
				wx.onMenuShareTimeline({
					title: txt, // 分享标题
					link: url, // 分享链接
					imgUrl: "http://tpn.gzl.com.cn/static/game/runningMonkey/image/card-monkey.png", // 分享图标
					type: '', 
					dataUrl: '', 
					success: function () { ;
						shareback();
					},
					cancel: function () { 
						// 用户取消分享后执行的回调函数
					}
				});		
				
			});
	
			wx.error(function(res){
				console.log("验证失败："+res);
			});
		},
	
		// 时间倒数
		countclock: function (obj,callback) {
			var that = this;
			that.counttimer = setInterval(function () {
				if (that.secondtime_nz <= 0) {
					callback();
					clearInterval(that.counttimer);
				} else {
					that.secondtime_nz--;
					that.changeForm(that.secondtime_nz, obj);
				}
			}, 1000)
		},
	
		// 将秒数转为分秒的形式
		changeForm : function(num , obj){
			//直接系统字体
			// var m = Math.floor(num/60);
			// min = m < 10 ? '0' + m : m;
			// var n = parseInt(num%60);
			// var second = n < 10 ? "0" + n : n;
			// second = second.toString();
			// obj.text(min + ':' + second);
			// 背景图片
			var min = Math.floor(num / 60);
			var n = parseInt(num % 60);
			var second = n < 10 ? "0" + n : n;
			second = second.toString();
			obj.find('span').eq(1).css({
				'background-position': min * 11 + "% 0%"
			});
			for (var i = 0; i < second.length; i++) {
				obj.find('span').eq(2+i).css({
					'background-position': second[i] * 11 + '% 0%'
				});
			}
	
		},
	
		// 将分数转为图片形式
		scoreChangePic: function(num,$obj){
			if (num < 10) {
				num = '0' + num;
			}else{
				num = '' + num;
			}
			for (var i = 0; i < num.length; i++) {
				$obj.eq(i).css({
					'background-position': num[i]*11+'% 0%'
				})
			}
		},
	
		//随机顺序
		randomOrder: function (arr) {
			//时间复杂度为 O(n)
			var len = arr.length;
			for (var i = 0; i < len - 1; i++) {
				var idx = Math.floor(Math.random() * (len - i));
				var temp = arr[idx];
				arr[idx] = arr[len - i - 1];
				arr[len - i - 1] = temp;
			}
			return arr;
	
			//时间复杂度为O(n2);
			// var result = [];
			// var ranNum = arr.length;
			// for (var i = 0; i < ranNum; i++) {
			// 	var ran = Math.floor(Math.random() * arr.length);
			// 	result.push(arr.splice(ran, 1)[0]);
			// };
			// return result;
		},
	
		// 铲雪
		shovelSnow: function(changeTime,snowballIndex){
			clearInterval(this.standtimer);
			var that = this;
			that.monkeytimer = setInterval(function () {
				if (changeTime >= 6 ) {
					clearInterval(that.monkeytimer);
					that.standOcation(3);
					setTimeout(function(){
						$('.monkey_box').css({
							'left': parseInt(snowballIndex * 8 - 7) + '%'
						})
					}, 800);
				} else {
					changeTime++;
				}
				$('.shovel_snow span').removeClass().addClass(function (index, oldclass){
					if (changeTime == 5) {
						$('.snowball' + snowballIndex).addClass('snowball_animation');
						
					}
					return 'monkey' + changeTime;
				});
			}, 200) 
		},
	
		// 站立
		standOcation: function(stand_time){
			this.standtimer = setInterval(function () {
				if (stand_time == 3) {
					// clearInterval(monkeytimer);
					stand_time=1;
				} else {
					stand_time++;
				}
				$('.shovel_snow span').removeClass().addClass('monkey' + stand_time);
			},400)
		}		
	
	}
